﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Container
 * 唯一标识：20c16c2d-90c2-4485-b8b7-6066c9ede1c9
 * 文件名：Card
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/7/25 11:27:32
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 卡片控件
/// </summary>
public sealed class Card : ContainerControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Card()
    {
        Children = new List<ControlBase>();
    }

    public override List<ControlBase> Children { get; internal set; }
    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendLine($"<el-card style=\"max-width: {MaxWidth}px\" shadow=\"{ShadowType.GetDescription()}\">");
        if (Header != null)
        {
            sb.AppendLine($"<template #header>");

            if (Header is string headerStr)
            {
                sb.AppendLine($"<div class=\"card-header\">");
                sb.AppendLine($"<span>{headerStr}</span>");
                sb.AppendLine($"</div>");
            }
            else if (Header is ControlBase cb)
            {
                sb.AppendLine(cb.ConverToHtml());
            }

            sb.AppendLine($"</template>");
        }

        foreach (var item in Children)
        {
            sb.AppendLine(item.ConverToHtml());
        }

        if (Footer != null)
        {
            sb.AppendLine($"<template #footer>");

            if (Footer is string footerStr)
            {
                sb.AppendLine($"{footerStr}");
            }
            else if (Footer is ControlBase cb)
            {
                sb.AppendLine(cb.ConverToHtml());
            }

            sb.AppendLine($"</template>");
        }

        sb.AppendLine($"");
        sb.AppendLine($"</el-card>");

        return sb.ToString();
    }

    /// <summary>
    /// 卡片最大宽度
    /// </summary>
    internal int MaxWidth { get; set; }

    /// <summary>
    /// 头部
    /// </summary>
    internal object? Header { get; set; }

    /// <summary>
    /// 底部
    /// </summary>
    internal object? Footer { get; set; }

    /// <summary>
    /// 阴影类型
    /// </summary>
    internal CardShadowTypeEnum ShadowType { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建卡片控件
    /// </summary>
    /// <param name="maxWidth">最大宽度</param>
    /// <param name="title">头部标题</param>
    /// <param name="content">内容</param>
    /// <param name="footer">底部内容</param>
    /// <param name="shadowType">阴影类型</param>
    /// <returns></returns>
    public static Card CreateCard(int maxWidth, string title, Action<List<ControlBase>> content, string footer, CardShadowTypeEnum shadowType = CardShadowTypeEnum.Always)
    {
        Card card = new Card()
        {
            MaxWidth = maxWidth,
            Header = title,
            Footer = footer,
            ShadowType = shadowType
        };

        content?.Invoke(card.Children);

        return card;
    }

    /// <summary>
    /// 创建卡片控件
    /// </summary>
    /// <param name="maxWidth">最大宽度</param>
    /// <param name="titleFunc">头部内容</param>
    /// <param name="content">内容</param>
    /// <param name="footerFunc">底部内容</param>
    /// <param name="shadowType">阴影类型</param>
    /// <returns></returns>
    public static Card CreateCard(int maxWidth, Func<ControlBase> titleFunc, Action<List<ControlBase>> content, Func<ControlBase> footerFunc, CardShadowTypeEnum shadowType = CardShadowTypeEnum.Always)
    {
        Card card = new Card()
        {
            MaxWidth = maxWidth,
            ShadowType = shadowType
        };

        card.Header = titleFunc?.Invoke();
        content?.Invoke(card.Children);
        card.Footer = footerFunc?.Invoke();

        return card;
    }
}

/// <summary>
/// 卡片阴影类型
/// </summary>
public enum CardShadowTypeEnum
{
    /// <summary>
    /// 始终有
    /// </summary>
    [Description("always")]
    Always,

    /// <summary>
    /// 没有
    /// </summary>
    [Description("never")]
    Never,

    /// <summary>
    /// 鼠标移动到上面才有
    /// </summary>
    [Description("hover")]
    Hover,
}